/* This is a file which defines some interfaces for datatype. */

#pragma once

#include <algorithm>
#include <iostream>
#include <string>

inline bool is_leap_year(int year){
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        return true;
    return false;
}

inline int string_to_int(std::string date_str){
    std::string data_str_format;
    int year = 0, month = 0, day = 0;
    int i = 0;
    for(i = 0; i < static_cast<int>(date_str.size()) && date_str[i] != '-'; i ++){
        year *= 10;
        year += date_str[i] - '0';
    }
    i++;    // 跳过'-'
    for(; i < static_cast<int>(date_str.size()) && date_str[i] != '-';i++){
        month *= 10;
        month += date_str[i] - '0';
    }
    i++;    // 跳过'-'
    for(;i < static_cast<int>(date_str.size()); i++){
        day *= 10;
        day += date_str[i] - '0';
    }
    int reg = 0;
    reg = year * 10000 + month * 100 + day;
    return reg;
}

inline std::string int_to_string(int date_int){
    std::string reg;
    int year = date_int / 10000;
    date_int %= 10000;
    int month = date_int / 100;
    date_int %= 100;
    int day = date_int;
    for(int i = 0; i < 2; i++){
        reg += day % 10 + '0';
        day /= 10;
    }
    reg += '-';
    for(int i = 0; i < 2; i++){
       reg += month % 10 + '0';
       month /= 10;
    }
    reg += '-';
    for(int i = 0; i < 4; i++){
       reg += year % 10 + '0';
       year /= 10;
    }
    std::reverse(reg.begin(), reg.end());
    return reg;
    // 最后统一转换成 yyyy-mm-dd 的格式
}


// 判断日期是否合法
inline bool is_valid(int date_int){
    int year = date_int / 10000;
    date_int %= 10000;
    int month = date_int / 100;
    date_int %= 100;
    int day = date_int;
    int month_day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if(is_leap_year(year)) month_day[2] = 29;   // 闰年2月29天
    if(month > 12 || month < 1) return false;
    if(day > month_day[month]|| day < 1) return false;
    return true;
}
